<?php

namespace app\admin\controller;

use think\Validate;
use think\Db;
use app\models\{User as Users, Address, Region, PlatClass, Config, Annou, Banner};

class System extends Base
{


    public function annous()
    {
        $list = (new Annou())->order(['sort' => 'desc', 'id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['name'])) {
            $list = $list->where('title', 'like', '%' . $data['name'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function annous_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'content' => 'require'
            ], [
                'title.require' => '标题不能为空',
                'content.require' => '内容不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new Annou())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Annou())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'code' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Annou())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function annous_del()
    {
        if ($this->request->isPost()) {
            $id = array_filter(input('id'));
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择项目']);
            }
            $banner = (new Annou())->where(['id' => $id])->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            $res = (new Annou())->where(['id' => $id])->delete();
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function PlatClass()
    {
        $list = (new PlatClass())->order(['sort' => 'desc', 'id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['name'])) {
            $list = $list->where('name', 'like', '%' . $data['name'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function platclass_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'images' => 'require'
            ], [
                'name.require' => '分类名称不能为空',
                'images.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new PlatClass())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new PlatClass())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new PlatClass())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function platclass_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $banner = (new PlatClass())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            $isshow = $banner['isshow'] == 1 ? 2 : 1;
            $res = (new PlatClass())->where('id', $id)->update(['isshow' => $isshow]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function banner()
    {
        $list = (new Banner())->order('id desc');
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'url' => 'require'
            ], [
                'title.require' => '轮播图备注信息不能为空',
                'url.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new Banner())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Banner())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Banner())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function banner_del()
    {
        if ($this->request->isPost()) {
            $id = array_filter(input('id'));
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择项目']);
            }
            $banner = (new Banner())->where(['id' => $id])->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            $res = (new Banner())->where(['id' => $id])->delete();
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function config()
    {
        $list = (new Config())->paginate(10);
        foreach ($list as $lists) {
            if ($lists['name'] == 'mybank') {
                $lists['value'] = json_decode($lists['value'], true);
            }
        }
        return view(null, [
            'list' => $list
        ]);
    }


    public function config_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'value' => 'require'
            ], [
                'name.require' => '名称不能为空',
                'value.require' => '路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if ($data['name'] == 'mybank') {
                $data['value'] = json_encode($data['value'], true);
            }
            $res = (new Config())->allowfield(true)->save($data, ['name' => $data['name']]);
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('name', '');
            $row = (new Config())->where('name', $id)->find();
            if (!empty($row) && $row['name'] == 'mybank') {
                $row['value'] = json_decode($row['value'], true);
            }
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function line()
    {
        return view();
    }
}
